package com.example.springboot.quartz.controller;

import com.alibaba.druid.util.StringUtils;
import com.example.springboot.quartz.QuartzBean;
import com.example.springboot.quartz.service.QuartzJobService;
import org.quartz.JobDataMap;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;

/**
 * @program: attend-server
 * @description:
 * @author: Yuwl
 * @create: 2020-07-21 17:08
 **/
@RestController
@RequestMapping("/api/quartz/")
public class JobController {

	@Autowired
	private QuartzJobService quartzJobService;


	//创建&启动
	@GetMapping("startSimpleJob")
	public String startSimpleJob() throws SchedulerException, ClassNotFoundException, ParseException {
		QuartzBean quartzBean = new QuartzBean();
		quartzBean.setJobClass("com.example.springboot.quartz.MyTask");
		quartzBean.setJobName("job1");
		JobDataMap map = new JobDataMap();
		map.put("userId", 123456);
		quartzBean.setJobDataMap(map);
		Date now = new Date();
		quartzBean.setStartTime(new Date());
		quartzBean.setInterval(1);
		//quartzBean.setEndTime(DateUtils.addMinutes(now, 1));
		try {
			quartzJobService.createScheduleJobSimple(quartzBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "startJob Success!";
	}

	/**
	 * 创建cron Job
	 * @param quartzBean
	 * @return
	 */
	@RequestMapping("/createCronJob")
	@ResponseBody
	public String  createJob(QuartzBean quartzBean)  {
		try {
			//进行测试所以写死
			quartzBean.setJobClass("com.example.springboot.quartz.MyTask");
			quartzBean.setJobName("job1");
			quartzBean.setCronExpression("*/5 * * * * ?");
			JobDataMap map = new JobDataMap();
			map.put("userId", 123456);
			quartzBean.setJobDataMap(map);
			quartzJobService.createScheduleJobCron(quartzBean);
		} catch (Exception e) {
			e.printStackTrace();
			return "创建失败";
		}
		return "创建成功";
	}

	/**
	 * 暂停job
	 * @return
	 */
	@RequestMapping(value = {"/pauseJob/{jobName}","/pauseJob/{jobName}/{jobGroup}"})
	@ResponseBody
	public String  pauseJob(@PathVariable("jobName") String  jobName, @PathVariable(required = false) String jobGroup)  {
		try {
			quartzJobService.pauseScheduleJob(jobName, !StringUtils.isEmpty(jobGroup) ?jobGroup:null);
		} catch (Exception e) {
			return "暂停失败";
		}
		return "暂停成功";
	}

	@RequestMapping(value = {"/resume/{jobName}","/resume/{jobName}/{jobGroup}"})
	@ResponseBody
	public String  resume(@PathVariable("jobName") String  jobName, @PathVariable(required = false) String jobGroup)  {
		try {
			quartzJobService.resumeScheduleJob(jobName,!StringUtils.isEmpty(jobGroup) ?jobGroup:null);
		} catch (Exception e) {
			return "启动失败";
		}
		return "启动成功";
	}

	@RequestMapping(value = {"/delete/{jobName}","/delete/{jobName}/{jobGroup}"})
	public String  delete(@PathVariable("jobName") String  jobName, @PathVariable(required = false) String jobGroup)  {
		try {
			quartzJobService.deleteScheduleJob(jobName,!StringUtils.isEmpty(jobGroup) ?jobGroup:null);
		} catch (Exception e) {
			return "删除失败";
		}
		return "删除成功";
	}

	@RequestMapping(value = {"/check/{jobName}","/check/{jobName}/{jobGroup}"})
	public String  check(@PathVariable("jobName") String  jobName, @PathVariable(required = false) String jobGroup)  {
		try {
			if(quartzJobService.checkExistsScheduleJob(jobName,!StringUtils.isEmpty(jobGroup) ?jobGroup:null)){
				return "存在定时任务："+jobName;
			}else{
				return "不存在定时任务："+jobName;
			}
		} catch (Exception e) {
			return "查询任务失败";
		}
	}

	@RequestMapping(value = {"/status/{jobName}","/status/{jobName}/{jobGroup}"})
	@ResponseBody
	public String  status(@PathVariable("jobName") String  jobName, @PathVariable(required = false) String jobGroup)  {
		try {
			return quartzJobService.getScheduleJobStatus(jobName,!StringUtils.isEmpty(jobGroup) ?jobGroup:null);
		} catch (Exception e) {
			return "获取状态失败";
		}
		//return "获取状态成功";
	}

}
